<?php

include_once APPLICATION_DIR . "/application/model/CATEGORY.php";

class Category extends DataObjects_CATEGORY
{
	public function validateInput(){
        global $error_msg;
        $posted_file = $_FILES["IMAGE"];
		if ($error_msg->isNoError() && !empty($posted_file["name"])) {
			$old_file = '';
			if ($this->ID) {
				$old_object = new self();
				$old_object->get($this->ID);
				$old_file = $old_object->IMAGE;
                if($old_file!=""){
                    @unlink(PRODUCT_DIR."/image/".$old_file);
                    @unlink(PRODUCT_DIR."/image/thumb".$old_file);
                }
                
			}
            $upload = new UPLOAD();
			$new_file = $upload->doUploadImage(PRODUCT_DIR."/image/", 'IMAGE', '', '190', '');
			if ($new_file) {
				$this->IMAGE = $new_file;
			} else {
				$error_msg->add('_MSG_UPLOAD_FAILED');
			}
		}
        return $error_msg->isNoError();
    }
	
	public static function isExist($title, $id = 0) {
		$category = new Category();
		$category->TITLE = $title;
		if (!empty($id)) {
			$category->whereAdd("ID <> $id");
		}
		$category->find();

		return (($category->N > 0) ? 1 : 0);
	}

	 public static function deletable($id) {
		// check if exists a child node?
		$cat = new Category();
		$cat->ID_PARENT = $id;
		if($cat->count()>0){
			return false;
		}
		
		// check if exist a product
		$product = new Product();
		$product->ID_CATEGORY = $id;
		if($product->count()>0){
			return false;
		}
		return true;
	}

	public static function getTree($id_parent = 0) {
		$tree = array();

		// get all childs of current id_parent
        $cat = new Category();
		$cat->ID_PARENT = $id_parent;
		$cat->orderBy('POS, TITLE');
		$cat->find();

        while($cat->fetch()){
            $tree[$cat->ID]['TITLE'] = $cat->TITLE;
            $tree[$cat->ID]['DESCRIPTION'] = $cat->DESCRIPTION;
            $tree[$cat->ID]['childs'] = self::getTree($cat->ID);
            $tree[$cat->ID]['deletable'] = empty($tree[$cat->ID]['childs']) ? true : false;
        }

		return $tree;
    }
    public static function getSubItem($id_parent){
        $cat = new Category();
        $sql = "SELECT * FROM CATEGORY WHERE ID_PARENT = {$id_parent} AND DISABLED = 0 ORDER BY POS ASC";
        $cat->query($sql);
        return $cat;
    }
    
    public static function getAllSubItem($id_parent,&$arrCat,$path){
        $cat = new Category();
        $sql = "SELECT * FROM CATEGORY WHERE ID_PARENT = {$id_parent} AND DISABLED = 0 ORDER BY POS ASC";
        $cat->query($sql);
        while($cat->fetch()){
            if(!isset($arrCat[$cat->ID_PARENT])){
                $arrCat[$cat->ID] = array();
            }
            $arrCat[$cat->ID] = array("ID"=> $cat->ID, "TITLE" => $cat->TITLE);
            if(in_array($cat->ID, $path)){
                $arrCat[$cat->ID]["HAVE_SUB"] = true;
                self::getAllSubItem($cat->ID,$arrCat[$cat->ID]["SUB"],$path);
            }
            else{
                $arrCat[$cat->ID]["HAVE_SUB"] = false;
            }
        }
    }
    
    public static function getCategoryPath($id,&$path){
        $cat = new Category();
        $cat->ID = $id;
        $cat->find(1);
        if($cat->ID_PARENT >0){
            $path[] = $cat->ID_PARENT;
            self::getCategoryPath($cat->ID_PARENT, $path);
        }       
    }
    public static function getCategoryNamePath($id,&$path){
        $cat = new Category();
        $cat->ID = $id;
        $cat->find(1);
        $path[] = array("ID"=>$cat->ID, "TITLE"=>$cat->TITLE) ;
        if($cat->ID_PARENT >0){
            self::getCategoryNamePath($cat->ID_PARENT, $path);
        }       
    }
}
?>
